package com.mdhw.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdhw.admin.dao.AdminShangwuRegionDao;
import com.mdhw.admin.entity.AdminShangwuRegion;
import com.mdhw.admin.service.AdminShangwuRegionService;
import com.mdhw.admin.service.AdminUserService;
import com.mdhw.admin.vo.AdminShangwuRegionTree;
import com.mdhw.admin.vo.AdminShangwuRegionVo;
import com.mdhw.common.enums.StatusEnum;
import com.mdhw.config.R;
import com.mdhw.system.dao.SysRegionDao;
import com.mdhw.system.service.SysRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【admin_shangwu_region(行政区域表_商务)】的数据库操作Service实现
* @createDate 2024-12-05 14:29:42
*/
@Service
public class AdminShangwuRegionServiceImpl extends ServiceImpl<AdminShangwuRegionDao, AdminShangwuRegion>
    implements AdminShangwuRegionService {

    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private SysRegionService sysRegionService;
    @Autowired
    private SysRegionDao sysRegionDao;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public R<String> setShangwuRegion(AdminShangwuRegionVo adminShangwuRegion) {
        Long adminUserId = adminShangwuRegion.getAdminUserId();
        LambdaQueryWrapper<AdminShangwuRegion> asrLqw = new LambdaQueryWrapper<>();
        asrLqw.eq(AdminShangwuRegion::getAdminUserId,adminUserId);
        List<AdminShangwuRegion> adminShangwuRegionList = this.list(asrLqw);
        Boolean isSuccess = Boolean.TRUE;
        for(AdminShangwuRegion adminShangwuRegion1:adminShangwuRegionList){
            isSuccess = this.removeById(adminShangwuRegion1.getId());
        }
        if(isSuccess){
            List<Long> sysRegionIdList = adminShangwuRegion.getSysRegionIdList();
            for(Long sysRegionId:sysRegionIdList){
                AdminShangwuRegion adminShangwuRegion1 = new AdminShangwuRegion();
                adminShangwuRegion1.setSysRegionId(sysRegionId);
                adminShangwuRegion1.setAdminUserId(adminUserId);
                this.save(adminShangwuRegion1);
            }
            return R.success();
        }else{
            return R.error("操作失败");
        }
    }



    private List<AdminShangwuRegionTree> getChild(Long parentId, List<AdminShangwuRegionTree> sysRegionTreeList){
        // 子菜单列表
        List<AdminShangwuRegionTree> childList = new ArrayList<>();
        for(AdminShangwuRegionTree sysMenuTree:sysRegionTreeList){
            if(sysMenuTree.getPid().toString().equals(parentId.toString())){
                childList.add(sysMenuTree);
            }
        }

        // 遍历 获取子菜单的子菜单
        for (AdminShangwuRegionTree menu : childList) {
            List<AdminShangwuRegionTree> child = getChild(menu.getId(), sysRegionTreeList);
            if(child != null && child.size() > 0){
                menu.setChildren(child);
            }
        }
        //递归出口 childList长度为0
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

}




